import axios from 'axios';
import {errorRes} from './check';
let base = process.env.API_ROOT;
// let base = 'http://web.ygcjp.com/web';
// let urlbase = 'http://web.ygcjp.com/wap';
let urlbase = 'http://116.62.158.253:8129';
// let urlbase='http://192.168.0.117:8129'
export {base,urlbase};
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

const trans = params => {
    // Do whatever you want to transform the data
    let ret = ''
    for (let it in params) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(params[it]) + '&'
    }
    return ret;
}
// get 请求
export const getReq = (api,params)=>{
	params = trans(params);
	return axios.get(`${base}${api}?${params}`).then(res=>res.data);
}
// post请求  json
export const postReq = (api,params)=>{
	// params = trans(params);
	return axios.post(`${base}${api}`,params).then(res=>res.data);
}

// post请求 
export const postReq2 = (api,params)=>{
	params = trans(params);
	return axios.post(`${base}${api}`,params).then(res=>res.data);
}

export const postKahoReq = (api,params)=>{
    params = trans(params);
    return axios.post(`http://web.ygcjp.com${api}`,params).then(res=>res.data);
}


// 更新用户信息
export const getUserInfo = ()=>{
    let params ={
        token: getCookie('token')
    }
    params = trans(params);
    return axios.post(`${base}/user/getuserinfo`,params).then(res=>{
        let {errcode,content,message} = res.data;
        if (errcode==0) {
            setCookie('token',content.token,24*7)
            return content;
        }else{
            errorRes(errcode,message);
            return {};
        }
    });
}
// 获取本地json
let local =  location.origin;
export const getLocalReq = (api)=>{
    return axios.get(`${local}/static/json${api}.json`).then(res=>res.data);
}